package com.weipai.springboot.module.service.impl;

import com.weipai.springboot.enums.RetCode;
import com.weipai.springboot.model.Shop;
import com.weipai.springboot.model.ShopCategory;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.dao.ShopCategoryDao;
import com.weipai.springboot.module.service.ShopCategoryService;
import com.weipai.springboot.module.util.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 曹朋飞
 * @Date: 2018/07/27 17:50
 */
@Service
public class ShopCategoryServiceImpl implements ShopCategoryService {

    private static final Logger log = LoggerFactory.getLogger(ShopCategoryServiceImpl.class);

    @Autowired
    ShopCategoryDao shopCategoryDao;
    
    //保存
    @Override
    public Response save(ShopCategory shopCategory) {
        Response response = new Response();
        if(1==shopCategoryDao.insert(shopCategory)){
            response.setCode(RetCode.SUCCESS);
        }else{
            log.debug("保存品牌失败:{}",shopCategory.getShopCategoryName());
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //修改
    @Override
    public Response update(ShopCategory shopCategory) {
        Response response = new Response();
        if(1==shopCategoryDao.updateByPrimaryKey(shopCategory)){
            response.setCode(RetCode.SUCCESS);
        }else{
            log.debug("修改商品类别失败:{}",shopCategory.getShopCategoryName());
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //查询店铺类别详情
    @Override
    public Response getShopCategoryById(String shopCategoryId) {
        Response response = new Response();
        ShopCategory shopCategory = shopCategoryDao.getShopCategoryById(shopCategoryId);
        if(shopCategory!=null){
            response.setCode(RetCode.SUCCESS);
            response.put("data",shopCategory);
        }else {
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }


    //获取数据（不包含逻辑删除的数据）
    @Override
    public List<Shop> selectAllByShopCategoryDel(int shopCategoryDel, String search, String sort, String way) {
        return shopCategoryDao.selectAllByShopCategoryDel(shopCategoryDel,search, sort, way);
    }

    //删除
    @Override
    @Transactional(readOnly=false, propagation = Propagation.REQUIRED)
    public Response deleteById(String shopCategoryId) {
        Response response = new Response();
        ShopCategory shopCategory = shopCategoryDao.getShopCategoryById(shopCategoryId);
        if(null!=shopCategory){
            shopCategory.setDelTime(DateUtils.timeFormat(System.currentTimeMillis()));
            shopCategory.setShopCategoryDel(-1);
            if(1==shopCategoryDao.updateByPrimaryKey(shopCategory)){
                response.setCode(RetCode.SUCCESS);
            }else{
                response.setCode(RetCode.REQUEST_ERROR);
            }
        }else {
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }

    //更改商品类别的状态
    @Override
    public Response changeStatus(String shopCategoryId, Integer status) {
        Response response = new Response();
        if(1==shopCategoryDao.updateStatus(shopCategoryId,status)){
            response.setCode(RetCode.SUCCESS);
        }else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //批量删除
    @Override
    public Response batchDeleteById(String[] id) {
        Response response = new Response();
        if(shopCategoryDao.batchDeleteById(id)>0){
            response.setCode(RetCode.SUCCESS);
        }else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }
}
